import resources.Constants;

import registnet.records.DefaultCourse;
import registnet.records.summary.freq.GradeFrequency;
import registnet.records.summary.freq.source.GradeFrequencySource;
import registnet.records.source.CollegeSource;
import registnet.records.College;

/* [start] 	initializes gui variables */
String _instance = bundle.getProperty("registnet.module.data_management.course");
String _instances = bundle.getProperty("registnet.module.data_management.courses");

String _academic_year = bundle.getProperty("registnet.module.summaries.macro.honorary_students.search.academic_year");
String _semester = bundle.getProperty("registnet.module.summaries.macro.honorary_students.search.semester");

String _registrar = Constants.REGISTRAR;
String _college = Constants.COLLEGE;
String _unit = Constants.UNIT;

String _selected = bundle.getProperty("registnet.messages.selected");
/* [end] */


List data = new Vector();
DefaultCourse course;

/* [start]	prepares the tree */
ArrayList parentList = new ArrayList();
Map mapSTN_parent = new HashMap();
Map mapAL_parent = new HashMap();
Map mapAL_child = new HashMap();
Map map_treeitem = new HashMap();
SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
/* [end] */



/* [start]	defines the renderer for the tree */
public class MyTreeitemRenderer implements TreeitemRenderer {
	public void render(Treeitem item, Object data) throws Exception {	
		String str = data.toString();
		Treerow tr = null;
		Treecell tc = new Treecell(str);
		if(item.getTreerow()==null){
			tr = new Treerow();
			tr.setParent(item);
			map_treeitem.put(str, item);
		}else{
			tr = item.getTreerow(); 
			tr.getChildren().clear();
		}
		tc.setParent(tr);
	}
}
/* [end] */


/* [start]	retrieves the courses and places them on the tree;
			limits the courses to retrieve depending on the user type */
public void retrieveObjects(){
	map_treeitem.clear();
	mapSTN_parent.clear();
	mapAL_parent.clear();
	mapAL_child.clear();
	parentList.clear();
	
	List units;
	if (_user_type.equals(_registrar))
		units = new registnet.dao.UnitPeer().getAllUnits();
	else if (_user_type.equals(_college))
		units = new registnet.dao.UnitPeer().getUnitByCollege(new CollegePeer().getCollegeId(_office));
	for(int i=0; i<units.size(); i++){
		List DAOCourses = new registnet.dao.CoursePeer().getCoursesByUnit(units.get(i).getUnitId());
		ArrayList alChild = new ArrayList();
		ArrayList alLeaf = new ArrayList();
		for(int j=0; j<DAOCourses.size(); j++){
			SimpleTreeNode stnChild = new SimpleTreeNode(DAOCourses.get(j).getCourseCode(), new ArrayList());
			alLeaf.add(DAOCourses.get(j).getCourseCode());
			alChild.add(stnChild);
		}
		SimpleTreeNode stnParent = new SimpleTreeNode(units.get(i).getUnitCode(), alChild);
		mapSTN_parent.put(units.get(i).getUnitCode(), stnParent);
		mapAL_parent.put(units.get(i).getUnitCode(), alChild);
		mapAL_child.put(units.get(i).getUnitCode(), alLeaf);
		parentList.add(stnParent);
	}
	SimpleTreeNode root = new SimpleTreeNode("ROOT", parentList);
	SimpleTreeModel stm = new SimpleTreeModel(root);
	instance_tree.setModel(stm);
	instance_tree.setTreeitemRenderer(new MyTreeitemRenderer());
}
/* [end] */


/* [start]	defines an event listener for the items on the tree;
			when the item is selected, its info is displayed */
private class SelectEventListener implements EventListener {
	public SelectEventListener (){}			
	public void onEvent(Event event) throws UiException {
		showInfo();
	}
}	
/* [end] */


/* [start]	automatically updates the 'to' value of academic year based on the
			current 'from' value of the academic year */
private void updateAY(){
	try{
		year2.value = (Integer.parseInt(event.value) + 1).toString();
	}catch(Exception e) {
		year1.value = null;
		year2.value = null;
	}
}
/* [end] */



private class FrequencyRenderer implements RowRenderer  {
	public void render(Row row, java.lang.Object data) {
		new Label(data[0]).setParent(row);
		new Label(data[1]).setParent(row);
	}
}


void submit(){
	Integer semesterValue = Constants.MAP_SEMESTER_KEY.get(semester.getSelectedItem().getLabel());
	Integer yearValue;
	if (semesterValue == Constants.MAP_SEMESTER_KEY.get(Constants.SUMMER))
		yearValue = Integer.parseInt(year2.value);
	else
		yearValue = Integer.parseInt(year1.value);
	
	HashMap map;
	if (_user_type.equals(Constants.REGISTRAR))
		 map = new GradeFrequencySource(course, semesterValue, yearValue).getGradeFrequency().getFrequencies();
	else if (_user_type.equals(Constants.COLLEGE)){
		College college = new CollegeSource(_office).getCollege();
		map = new GradeFrequencySource(college, course, semesterValue, yearValue).getGradeFrequency().getFrequencies();
	}
	
	String[][] listModel = new String[map.size()][2];
	for(int i=0; i<Constants.GRADE.length; i++){
		listModel[i][0] = Constants.GRADE[i];
		listModel[i][1] = "" + map.get(Constants.GRADE[i]);
	}
	ListModel strset = new SimpleListModel(listModel);
	contents_win.getFellow("frequency_g").setModel(strset);
	contents_win.getFellow("frequency_g").setRowRenderer(new FrequencyRenderer());
}

void showInfo(){
	if (contents.getFirstChild() != null){
		contents.removeChild(contents.getFirstChild());
	}
	
	if (instance_tree.getSelectedCount() == 1){
		if (instance_tree.getSelectedItem().getParentItem() != null){
			registnet.dao.Course DAOCourse = new registnet.dao.CoursePeer().getCourse(instance_tree.getSelectedItem().getLabel());
			course = new DefaultCourse(DAOCourse.getCourseCode());			
			title.setValue(DAOCourse.getCourseCode());
			Component show_instance_form = Executions.createComponents("/WEB-INF/macros/summaries/grade_frequency_form.zul", contents, null);
		}
		else
			title.setValue("");
	}
	else if (instance_tree.getSelectedCount() > 1)
		title.setValue("" + instance_tree.getSelectedCount() + " " + _instance + " " + _selected);
	else
		title.setValue("");
}


